﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Expo.Training.CustomerData;
using System.Data.SqlClient;
using System.Data;

namespace Expo.Training.DataAccess.Vip
{
    public class VipModule
    {
        #region 添加level
        public static bool AddLevel(LevelEntity model)
        {
            Database objDB = new Database();
            bool isSuccess = false;

            try
            {
                SqlParameter[] prams = {
                    
                    objDB.MakeInParam("@Title", System.Data.SqlDbType.NVarChar, 200, model.Title),
                    objDB.MakeInParam("@Score", System.Data.SqlDbType.Int, 4, model.Score)

                };
                objDB.RunProc("sp_Vip_Level_XB_Add", prams);

                isSuccess = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                objDB.Close();
                objDB.Dispose();
            }

            return isSuccess;
        }
        #endregion

        #region 更新level
        public static bool UpdateLevel(LevelEntity model)
        {
            Database objDB = new Database();
            bool isSuccess = false;

            try
            {
                SqlParameter[] prams = {
                    
                    objDB.MakeInParam("@Title", System.Data.SqlDbType.NVarChar, 200, model.Title),
                    objDB.MakeInParam("@Score", System.Data.SqlDbType.Int, 4, model.Score),
                    objDB.MakeInParam("@CustomCondition", System.Data.SqlDbType.NVarChar, 500, "LevelID ='" + model.LevelID.ToString() + "'")
                };
                objDB.RunProc("sp_Vip_Level_XB_Update", prams);

                isSuccess = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                objDB.Close();
                objDB.Dispose();
            }

            return isSuccess;
        }
        #endregion

        #region 删除level
        public static bool DeleteLevel(Guid id)
        {
            Database objDB = new Database();
            bool isSuccess = false;

            try
            {
                SqlParameter[] prams = {
                    
                    objDB.MakeInParam("@LevelID", System.Data.SqlDbType.UniqueIdentifier, 32, id)

                };
                objDB.RunProc("sp_Vip_Level_XB_Delete", prams);

                isSuccess = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                objDB.Close();
                objDB.Dispose();
            }

            return isSuccess;
        }
        #endregion

        #region 按指定条件查询等级信息
        public static List<LevelEntity> GetLevels(string condition, string sortExpression)
        {
            List<LevelEntity> _list = new List<LevelEntity>();

            Database objDB = new Database();
            SqlDataReader objReader = null;
            string _sortExpression;

            if (string.IsNullOrEmpty(sortExpression))
            {
                _sortExpression = "Score ASC";
            }
            else
            {
                _sortExpression = sortExpression;
            }

            try
            {
                if (string.IsNullOrEmpty(condition))
                {
                    SqlParameter[] prams = { objDB.MakeInParam("@SortExpression", System.Data.SqlDbType.NVarChar, 100, _sortExpression) };
                    objDB.RunProc("sp_Vip_Level_XB_Query", prams, out objReader);
                }
                else
                {
                    SqlParameter[] prams = {
                        objDB.MakeInParam("@CustomCondition", System.Data.SqlDbType.VarChar, 200, condition),
                        objDB.MakeInParam("@SortExpression", System.Data.SqlDbType.NVarChar, 100, _sortExpression) 
                    };
                    objDB.RunProc("sp_Vip_Level_XB_Query", prams, out objReader);

                }

                while (objReader.Read())
                {
                    LevelEntity _model = new LevelEntity();

                    _model.LevelID = new Guid(objReader["LevelID"].ToString().Trim());
                    _model.Title = objReader["Title"].ToString().Trim();
                    _model.Score = int.Parse(objReader["Score"].ToString().Trim());
                    _model.AddTime = DateTime.Parse(objReader["AddTime"].ToString());

                    _list.Add(_model);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                objDB.Close();
                objDB.Dispose();
            }

            return _list;
        }
        #endregion

        #region 根据id获取等级信息
        public static LevelEntity GetLevel(Guid id)
        {
            try
            {
                List<LevelEntity> _list = new List<LevelEntity>();
                string _condition = "LevelID='" + id.ToString() + "'";
                _list = GetLevels(_condition, string.Empty);

                LevelEntity _model = new LevelEntity();
                if (_list.Count > 0)
                {
                    _model = _list[0];
                }

                return _model;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 根据Score获取等级信息
        public static LevelEntity GetLevel(int score)
        {
            try
            {
                List<LevelEntity> _list = new List<LevelEntity>();
                string _condition = "Score<=" + score.ToString();
                _list = GetLevels(_condition, "Score DESC");

                LevelEntity _model = new LevelEntity();
                if (_list.Count > 0)
                {
                    _model = _list[0];
                }

                return _model;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 添加vip
        public static bool AddVip(VipEntity model)
        {
            Database objDB = new Database();
            bool isSuccess = false;

            try
            {
                SqlParameter[] prams = {
                    
                    objDB.MakeInParam("@MemberID", System.Data.SqlDbType.UniqueIdentifier, 32, model.MemberID),
                    objDB.MakeInParam("@Category", System.Data.SqlDbType.Int, 4, model.Category)

                };
                objDB.RunProc("sp_Member_Vip_XB_Add", prams);

                isSuccess = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                objDB.Close();
                objDB.Dispose();
            }

            return isSuccess;
        }
        #endregion

        #region 更新vip
        public static bool UpdateVip(VipEntity model)
        {
            Database objDB = new Database();
            bool isSuccess = false;

            try
            {
                SqlParameter[] prams = {
                    objDB.MakeInParam("@Category", System.Data.SqlDbType.Int, 4, model.Category),
                    objDB.MakeInParam("@UpgradeTime", System.Data.SqlDbType.SmallDateTime, 4, DateTime.Now),
                    objDB.MakeInParam("@VipID", System.Data.SqlDbType.UniqueIdentifier, 32, model.VipID)

                };
                objDB.RunProc("sp_Member_Vip_XB_Update", prams);

                isSuccess = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                objDB.Close();
                objDB.Dispose();
            }

            return isSuccess;
        }
        #endregion

        #region 按指定条件查询vip信息
        public static List<VipEntity> GetVips(string condition)
        {
            List<VipEntity> _list = new List<VipEntity>();

            Database objDB = new Database();
            SqlDataReader objReader = null;

            try
            {
                if (string.IsNullOrEmpty(condition))
                {
                    SqlParameter[] prams = { };
                    objDB.RunProc("sp_Member_Vip_XB_Query", prams, out objReader);
                }
                else
                {
                    SqlParameter[] prams = { objDB.MakeInParam("@CustomCondition", System.Data.SqlDbType.VarChar, 200, condition) };
                    objDB.RunProc("sp_Member_Vip_XB_Query", prams, out objReader);
                    
                }

                while (objReader.Read())
                {
                    VipEntity _model = new VipEntity();

                    _model.VipID = new Guid(objReader["VipID"].ToString().Trim());
                    _model.MemberID = new Guid(objReader["MemberID"].ToString().Trim());
                    _model.Category = int.Parse(objReader["Category"].ToString().Trim());
                    _model.AddTime = DateTime.Parse(objReader["AddTime"].ToString());


                    if (string.IsNullOrEmpty(objReader["UpgradeTime"].ToString()))
                    {
                        _model.UpgradeTime = null;
                    }
                    else
                    {
                        _model.UpgradeTime = DateTime.Parse(objReader["UpgradeTime"].ToString());
                    }

                    _list.Add(_model);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                objDB.Close();
                objDB.Dispose();
            }

            return _list;
        }
        #endregion

        #region 根据MemberID获取vip信息
        public static VipEntity GetVipByMemberID(Guid id)
        {
            try
            {
                List<VipEntity> _list = new List<VipEntity>();
                string _condition = "MemberID='" + id.ToString() + "'";
                _list = GetVips(_condition);

                VipEntity _model = new VipEntity();

                if (_list.Count > 0)
                {
                    _model = _list[0];
                }

                return _model;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}
